package itypes

import (
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo/random"
	"github.com/hwcer/cosgo/slice"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/errors"
)

var Chip = NewItemsIType(config.ITypeChip)

func init() {
	Chip.stacked = ITypeUNStacked
	Chip.SetAttach(chipCreator)
}

func chipCreator(u *updater.Updater, item *model.Items) (any, error) {
	c := config.Data.Chip[item.IID]
	if c == nil {
		return nil, errors.Errorf(0, "chip config not found:%v", item.IID)
	}
	//item.Value = 1
	//item.MainAttr = map[int32]*model.ChipAttr{}
	//item.OtherAttr = map[int32]*model.ChipOtherAttr{}
	//item.ExtraAttr = map[int32]*model.ChipExtraAttr{}
	item.Attach.Set(model.AttachChipTime, u.Now().Unix())
	switch c.ChipKind {
	case 2:
		return chipCreatorNewAttr2(u, item)
	default:
		return chipCreatorNewAttr1(u, item)
	}
}

// 普通
func chipCreatorNewAttr1(u *updater.Updater, item *model.Items) (any, error) {
	//主属性
	c := config.Data.Chip[item.IID]
	//r := config.GetChipAttrGroupRandom(c.MainGroupId)
	chipAttr := config.Data.ChipAttr[c.MainGroupId]
	if chipAttr == nil {
		return nil, errors.Errorf(0, "chipAttr config not found:%v", c.MainGroupId)
	}
	mainAttr := map[int32]*model.ChipAttr{}
	if attr, _ := randomChipAttr(c.MainGroupId); attr != nil {
		mainAttr[attr.Id] = attr
	}
	_, _ = item.Attach.Marshal(model.AttachChipMainAttr, mainAttr)

	//副属性
	subRandom := config.GetChipSubAttrLimitRandom(item.IID)
	if subRandom == nil {
		return item, nil
	}
	num := subRandom.Roll()
	if num <= 0 {
		return item, nil
	}

	otherAttrs := map[int32]*model.ChipOtherAttr{}
	for i := int32(0); i < num; i++ {
		if attr, weight := randomChipAttr(c.SubGroupId); attr != nil {
			otherAttr := &model.ChipOtherAttr{ChipAttr: *attr}
			otherAttr.Weight = weight
			otherAttrs[attr.Id] = otherAttr //随机重复的 被覆盖了 就好玩了
		}
	}
	_, _ = item.Attach.Marshal(model.AttachChipOtherAttr, otherAttrs)

	return item, nil
}

// 特殊
func chipCreatorNewAttr2(u *updater.Updater, item *model.Items) (any, error) {
	//从主属性里roll出3个不重复的属性加入到extraAttr里
	//主属性
	c := config.Data.Chip[item.IID]
	chipAttr := config.Data.ChipAttr[c.MainGroupId]
	if chipAttr == nil {
		return nil, errors.Errorf(0, "chipAttr config not found:%v", c.MainGroupId)
	}
	attrRandom := config.GetChipAttrGroupRandom(c.MainGroupId)
	if attrRandom == nil {
		return item, nil
	}
	arr := attrRandom.Multi(3)
	extraAttr := map[int32]*model.ChipExtraAttr{}

	for _, i := range arr {
		attr := chipAttr.Attr[int(i)]
		data := chipCreatorNewAttrRandomValue(attr.Id, attr.Value)
		extraAttr[attr.Id] = &model.ChipExtraAttr{ChipAttr: *data}
	}

	_, _ = item.Attach.Marshal(model.AttachChipExtraAttr, extraAttr)
	return item, nil
}

func randomChipAttr(id int32) (*model.ChipAttr, int32) {
	chipAttr := config.Data.ChipAttr[id]
	chipAttrRandom := config.GetChipAttrGroupRandom(id)
	if i := chipAttrRandom.Roll(); i >= 0 {
		attr := chipAttr.Attr[int(i)]
		return chipCreatorNewAttrRandomValue(attr.Id, attr.Value), attr.Weight
	}
	return nil, 0
}

func chipCreatorNewAttrRandomValue(id int32, s string) *model.ChipAttr {
	r := &model.ChipAttr{Id: id}
	arr := slice.SplitInt32(s, ",")
	if len(arr) < 2 {
		return r
	}
	r.Value = random.Roll(arr[0], arr[1])
	return r
}
